
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 <title>接口调试</title>
 <script src="../test/js/jquery-3.1.1.min.js"></script>
 <script src="../test/crypto-js/crypto-js.js"></script>
</head>

<body>
<p>请求地址：</p>
<input type="text" id="loc" value="http://localhost:8080/appInterfaceAES/appvest/order/getIconList.jhtml" style="width:1000px;height:30px;"/>
<p>请求类型：（post/get）</p>
<input type="text" id="type" value="post"  style="width:1000px;height:30px;"/>
<p>参数：（如：111=111&222=222）</p>
<input type="text" id="para"  style="width:1000px;height:30px;"/>
<p>需解密内容：（单独使用）</p>
<input type="text" id="jiemi"  style="width:1000px;height:30px;"/>
<h1>链接</h1>
<h3 style="word-wrap:break-word" id="link"></h3>
<button type="button" onClick="getParam()">点我生成加密连接/解密内容</button>
<h1>运行结果</h1>
<h3 style="word-wrap:break-word" id="result"></h3>
<h1>解密结果</h1>
<h3 style="word-wrap:break-word" id="jm"></h3>

</body>
<script>

var keyStr = 'E20D383FE1E53A1D';

 function encrypt(word,keys){
	keyStr = keyStr ? keyStr : 'E20D383FE1E53A1D';
var key = CryptoJS.enc.Utf8.parse(keyStr);//Latin1 w8m31+Yy/Nw6thPsMpO5fg==
var srcs = CryptoJS.enc.Utf8.parse(word);
var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
return encrypted.toString();
 }

 //解密
function decrypt(word, keyStr){
keyStr = keyStr ? keyStr : 'E20D383FE1E53A1D';
var key = CryptoJS.enc.Utf8.parse(keyStr);//Latin1 w8m31+Yy/Nw6thPsMpO5fg==
var decrypt = CryptoJS.AES.decrypt(word, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}

function getParam() {
    if ($("#jiemi").val() != "") {
        var res = decrypt($("#jiemi").val(), keyStr);
        $("#jm").text(res);
        $("#result").text($("#jiemi").val());
    } else {
        var times = new Date().getTime();
        var key_size = 128;
        var key_type = 'AES';
        var params = {};
        params.request_time = times;
        var json = JSON.stringify(params);


        function getSign(params, data) {
            var biao = '|$$|';
            var sign = '';
            for (var val in params) {
                sign += params[val] + biao;
            }
            params.sign = encrypt(sign, keyStr);
        }

        $("#result").text("");
        $("#link").text("");
        $("#jm").text("");
        var para = $("#para").val();
        var loc = $("#loc").val();
        var type = $("#type").val();
        if (para != "") {
            var value = para.split("&");
            if (typeof(value) != "undefined") {
                for (var i = 0; i < value.length; i++) {
                    var val = value[i].split("=");
                    params[val[0]] = val[1];
                }
            }
        }

        //获取签
        getSign(params);
        var par = loc + "?";
        for (var val1 in params) {
            par += val1 + '=' + params[val1] + '&';
        }
        par = par.substr(0, par.length - 1);
        $("#link").text(par);
        $.ajax({
            url: loc,
            data: {param: JSON.stringify(params)},
            type: type,
            dataType: "json",
            success: function (result) {
                var res = decrypt(result.data, keyStr);
                $("#jm").text(res);
                $("#result").text(JSON.stringify(result));
            }
        });
    }
}
</script>
</html>